<?php


namespace App\Http\Requests\Customer\Product\Index;


use App\Models\HsCode;
use App\Models\Positive;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreRequest extends FormRequest
{
    /**
     * @return array
     * @author: luffyzhao@vip.126.com
     * @datetime: 2019/3/28 16:00
     */
    public function rules(): array
    {
        return array_merge([
            'images' => ['nullable', 'array', 'max:10'],
        ], $this->commonRules(), $this->elementRules(request()->input('hs_code')));
    }

    /**
     * 模板要素上传验证
     * @param $hsCode
     * @return array
     */
    public function elementRules($code)
    {
        $hsCode = HsCode::query()->where('code', '=', $code)->first();
        return [
            'element' => ['required', 'max:255', function ($attribute, $value, $fail) use ($hsCode) {
                $el = count(explode('|', $value));
                if ($el - count($hsCode->element) !== 0) {
                    return $fail("请对比一下申报要素");
                }
            }],
            'qy1' => ['required', 'numeric', function ($attribute, $value, $fail) use ($hsCode) {
                if($hsCode->getAttribute('unit1') === '035'){
                    if($value !== $this->input('net_weight')){
                        return $fail('法定第一数量不等于净重!');
                    }
                }
            }],

            'qy2' => [Rule::requiredIf(function () use ($hsCode) {
                return !empty($hsCode->getAttribute('unit2'));
            }), 'numeric', function ($attribute, $value, $fail) use ($hsCode) {
                if($hsCode->getAttribute('unit2') === '035'){
                    if($value !== $this->input('net_weight')){
                        return $fail('法定第二数量不等于净重!');
                    }
                }
            }],
        ];
    }

    /**
     * 模板文件上传的验证
     * @return array
     */
    public function commonRules()
    {

        return [
            'sku' => ['required', 'min:2', 'alpha_dash', 'max:64', Rule::unique('products', 'sku')
                ->where('customer_id', request()->input('customer_id'))],
            'barcode' => ['required', 'min:2', 'alpha_dash', 'max:64'],
            'name' => ['required', 'string', 'max:255'],
            'net_weight' => ['required', 'numeric'],
            'weight' => ['required', 'numeric', 'gt:net_weight'],
            'brand' => ['required', 'string', 'max:50'],
            'hs_code' => ['required', 'size:10', Rule::exists('hs_codes', 'code'), function ($attribute, $value, $fail) {
                $count = Positive::query()
                    ->where('code', "=", substr($value, 0, 8))
                    ->count();
                if ($count === 0) {
                    $fail('正面清单不存在!');
                }
            }],
            'country' => ['required', 'size:3', Rule::exists('country', 'code')],
            'unit' => ['required', 'size:3', Rule::exists('unit', 'code')],
            'currency' => ['required', 'size:3', Rule::exists('currency', 'code')],
            'model' => ['nullable', 'max:255'],
            'specs' => ['nullable', 'max:255'],
            'function' => ['nullable', 'max:255'],
            'use' => ['nullable', 'max:255'],
            'ingredient' => ['nullable', 'max:255'],
        ];
    }

}
